package org.nf.trading.controller.app.user;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.controller.BaseController;
import org.nf.trading.dto.CommentMessageDTO;
import org.nf.trading.dto.MessageDTO;
import org.nf.trading.dto.UserCodeDTO;
import org.nf.trading.dto.UserDTO;
import org.nf.trading.entity.User;
import org.nf.trading.service.app.message.websocket.NotifyWebSocketServerHandler;
import org.nf.trading.service.app.user.UserService;
import org.nf.trading.service.app.user.impl.UserServiceImpl;
import org.nf.trading.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/user")
@CrossOrigin
public class UserController extends BaseController {
    private final UserServiceImpl userService;
    @PostMapping("/uids")
    public ResultVO getUserById(@RequestBody MessageDTO[] users){
        List<CommentMessageDTO> commentMessageDTOS = userService.listUser(users);
        return success(commentMessageDTOS);
    }
    /**
     * 通过密码登录
     * @return
     */
    @PostMapping("/loginPwd")
    public ResultVO loginByPwd(@RequestBody User user, HttpServletRequest request){
        log.info("手机号：{},密码：{}",user.getUserPhone(),user.getUserId());
        UserDTO userDTO = userService.loginAuthPwd(user.getUserPhone(), user.getUserPwd(), request);
        return success(userDTO);
    }

    /**
     * 生成验证码
     * @return
     */
    @GetMapping("/code")
    public ResultVO generateCode(String phone){
        userService.generateCode(phone);
        return success(200,"成功",null);
    }

    /**
     *
     * 登录
     * @param
     * @return
     */
    @PostMapping("/loginCode")
    public ResultVO login(@RequestBody UserCodeDTO user){
        log.info("手机号：{}",user.getPhone());
        UserDTO userDTO = userService.loginAuth(user.getPhone(),user.getCode());
//        response.setHeader("token", userDTO.getToken());
        return success(userDTO);
    }

    /**
     * 通过用户id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/user")
    public ResultVO getUser(@RequestAttribute("uid") int id){
        log.info("用户id为：{}",id);
        User user = userService.selectByPrimaryKey(id);
        return success(200,"查询成功",user);
    }

    @GetMapping("/verifyToken")
    public ResultVO verifyToken(){
        return success(200,"验证成功",null);
    }
}
